package cn.tjnext.apply.service;

import cn.tjnext.apply.dao.ApplicantDao;
import cn.tjnext.apply.entity.ApplicantEntity;
import cn.tjnext.apply.entity.ProductEntity;
import cn.tjnext.apply.repositories.ApplicantRepository;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by cp on 2017/8/4.
 */
@Service
@Transactional(readOnly = true)
public class ApplicantService extends AbstractService<ApplicantEntity, ApplicantRepository, ApplicantDao> {

    @Autowired
    private ApplicantDao applicantDao;

    @Override
    protected ApplicantDao getDao() {
        return applicantDao;
    }

    /**
     * 导出excel
     * @return
     * @param productList
     */
    public HSSFWorkbook export(List<ProductEntity> productList) {
        List<ApplicantEntity> applicantEntities = (List<ApplicantEntity>) applicantDao.findAllByStatus();
        if (applicantEntities.size() == 0) {
            return null;
        }
        HSSFWorkbook workbook = new HSSFWorkbook();
        String title = "报名信息表";
        String[] headers = {"序号","订单号","路线","类型","价格(元)","订单总价(元)","姓名","性别","手机号","证件号","联系地址","报名时间"};
        HSSFSheet sheet = workbook.createSheet("报名信息");
        // 表头样式
        HSSFCellStyle contentTitleStyle = workbook.createCellStyle();
        contentTitleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 上边框
        contentTitleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边框
        contentTitleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
        contentTitleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框
        contentTitleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
        contentTitleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
        HSSFFont contentTitleFont = workbook.createFont();
        contentTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 加粗
        contentTitleFont.setFontHeightInPoints((short) 12); // 12号字
        contentTitleStyle.setFont(contentTitleFont);
        // 表头
        HSSFRow headerRow = sheet.createRow(1);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(contentTitleStyle);
            sheet.setColumnWidth(0, 1300);
            sheet.setColumnWidth(1, 4200);
            sheet.setColumnWidth(2, 5000);
            sheet.setColumnWidth(3, 4200);
            sheet.setColumnWidth(4, 6200);
            sheet.setColumnWidth(5, 5000);
            sheet.setColumnWidth(6, 4200);
            sheet.setColumnWidth(7, 4200);
            sheet.setColumnWidth(8, 4200);
            sheet.setColumnWidth(9, 6200);
            sheet.setColumnWidth(10, 5000);
            sheet.setColumnWidth(11, 5000);
        }
        // 设置内容
        HSSFCellStyle contentStyle = workbook.createCellStyle();
        contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 上边框
        contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边框
        contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
        contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框
        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
        contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
        HSSFFont contentFont = workbook.createFont();
        contentFont.setFontHeightInPoints((short) 12); // 12号字
        contentStyle.setFont(contentFont);
        int rowNum = 1;
        for (ApplicantEntity applicant : applicantEntities) {
            HSSFRow row = sheet.createRow(++rowNum);
            int cellNum = 0;
            HSSFCell cell = row.createCell(cellNum++);
            cell.setCellValue(rowNum - 1);
            cell.setCellStyle(contentStyle); // 序号
            cell = row.createCell(cellNum++);
            cell.setCellValue(longNotNull(applicant.getOrderId()));
            cell.setCellStyle(contentStyle); // 订单号
            cell = row.createCell(cellNum++);
            for (ProductEntity productEntity : productList) {
                if (productEntity.getId().equals(applicant.getProductId())) {
                    cell.setCellValue(stringNotNULL(productEntity.getName()));
                    cell.setCellStyle(contentStyle); // 路线
                }
            }
            cell = row.createCell(cellNum++);
            cell.setCellValue(stringNotNULL(applicant.getTypeName()));
            cell.setCellStyle(contentStyle); // 类型
            cell = row.createCell(cellNum++);
            cell.setCellValue(stringNotNULL(applicant.getTypePrice()));
            cell.setCellStyle(contentStyle); // 价格(元)
            cell = row.createCell(cellNum++);
            cell.setCellValue(doubleNotNull(applicant.getOrderPrice()));
            cell.setCellStyle(contentStyle); // 订单总价(元)
            cell = row.createCell(cellNum++);
            cell.setCellValue(stringNotNULL(applicant.getName()));
            cell.setCellStyle(contentStyle); // 姓名
            cell = row.createCell(cellNum++);
            cell.setCellValue(intNotNull(applicant.getGender()) == 1 ? "男" : "女");
            cell.setCellStyle(contentStyle); // 性别
            cell = row.createCell(cellNum++);
            cell.setCellValue(stringNotNULL(applicant.getPhone()));
            cell.setCellStyle(contentStyle); // 手机号
            cell = row.createCell(cellNum++);
            cell.setCellValue(stringNotNULL(applicant.getCard()));
            cell.setCellStyle(contentStyle); // 证件号
            cell = row.createCell(cellNum++);
            cell.setCellValue(stringNotNULL(applicant.getAddress()));
            cell.setCellStyle(contentStyle); // 联系地址
            cell = row.createCell(cellNum++);
            cell.setCellValue(DateFormatUtils.format(applicant.getCreateTime(), "yyyy年MM月dd日"));
            cell.setCellStyle(contentStyle); // 报名时间
        }
        return workbook;
    }

    /**
     * 字符串非空验证
     * @return
     */
    private String stringNotNULL(String str) {
       return str == null ? "" : str;
    }

    /**
     * 数字验证非空
     * @param i
     * @return
     */
    private Integer intNotNull(Integer i) {
        return i == null ? 0 : i;
    }
    /**
     * 数字验证非空
     * @param i
     * @return
     */
    private Long longNotNull(Long i) {
        return i == null ? 0 : i;
    }
    /**
     * 数字验证非空
     * @param i
     * @return
     */
    private Double doubleNotNull(Double i) {
        return i == null ? 0 : i;
    }

    /**
     * 按ID查询报名信息
     * @param userId
     * @return
     */
    public List<ApplicantEntity> findByUserId(Long userId) {
        return getDao().findByUserId(userId);
    }

    /**
     * 按订单id查询报名信息
     * @param orderId
     * @return
     */
    public List<ApplicantEntity> findByOrderId(Long orderId) {
        return getDao().findByOrderId(orderId);
    }
}
